<?php
	/**
	* Eko 
	* 
	* Eko is the controller, it executes the route methods, loads the models and executes the methods in Kiosk model. 
	* Ads Controller executes the tasks for login, logout from ekokiosk device
	*
	* 
	* @author Thang
	* @version $Id:  ads.php 2012-04-18
	* @access public 
	*
	*/	
	class Eko extends CI_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('kiosk_model');
		}
		
		/**
		* index(): 	display the login screen for testing. Not use in ekokiosk device			
		* 
		* @param 
		* @return    
		* @access public
		*/
		public function index(){
			// Only show for testing
		$this->load->helper("form");
			$data['title'] = "Login Panel";			
			$this->load->view('eko/login', $data);
		}
		
		
		/**
		* login(): accept the username and password via HTTP POST to check login				
		* 
		* @param   
		* @return   0 or 1
					0: Login failed
					1: Login success
		* @access public
		*/
		public function login(){			
			$username 	= trim($this->input->post('myusername'));
			$password 	= trim($this->input->post('mypassword'));
			
			if(empty($username) || empty($password)) echo 0;
			
			$result 	= $this->kiosk_model->login($username, $password);
			
			// login success, store the username and password to session
			if($result){
				//print_r($result);exit();
				$kiosk_id =  $result['id'];
				$this->session->set_userdata("myusername",$username);
				$this->session->set_userdata("mypassword", $password);	
				$this->session->set_userdata("myid", $kiosk_id);				
				echo 1;
				//redirect("func/login_success", "refresh");
			}
			else{ echo 0; }
		}	
		
		
		/**
		* logout(): exit the system and remove the session data				
		* 
		* @param   
		* @return   					
		* @access public
		*/
		public function logout(){
			if($this->session->userdata('myusername')){
				$this->session->unset_userdata('myusername');
				$this->session->unset_userdata('mypassword');
				$this->session->unset_userdata('myid');
				redirect("eko", "refresh");
			}
		}
	}	
?>